// 每个视图(组件)有一个watcher用于观察属性是否更新，每个属性都有一个dep用于收集依赖
// 同一个组件中的一个watcher对应多个dep，不同组件中相同的属性(对应的dep)对应不同组件中的每个watcher
// watcher和dep是双向的关系

let id = 0;
class Dep {
  constructor() {
    this.id = id++;
    this.subs = []; //存放watcher
  }
  depend() {
    Dep.target.addDep(this) //让watcher记住dep
    // this.sub.push(Dep.target);
    // debugger
  }
  addSub(watcher){
    this.subs.push(watcher)
  }
  notify(){
    this.subs.forEach(watcher=>watcher.update()) //告诉watcher要更新了
  }
}
Dep.target = null;
let starck = []
export function pushTarget(watcher){
  starck.push(watcher)
  Dep.target = watcher
}
export function popTarget(){
  starck.pop()
  Dep.target = starck[starck.length - 1]
}
export default Dep;
